#!/usr/bin/env dash
# neeasade
# depends on: colort, wmutils opt/chwb2
# makes dual borders based on your bspwm colors

focusedTint=80
normalTint=20

# set border widths, if odd outer is 1px larger
borderWidth=`bspc config border_width`
inWidth=$(( $borderWidth/2 ))
outWidth=$(( ($borderWidth/2) + ($borderWidth%2) ))

# _chwb2 [Focused|Normal] [nodeId]
_chwb2() {
    colorType=$1
    shift
    eval chwb2 -I \$inner$colorType -O \$outer$colorType -i $inWidth -o $outWidth $@ 2>/dev/null &
    eval echo chwb2 -I \$inner$colorType -O \$outer$colorType -i $inWidth -o $outWidth $@ 2>/dev/null &
}

# define border colors based on bspwm border settings
outerFocused=`bspc config focused_border_color | tr -d \#`
colort -c "$outerFocused" && sign=+ || sign=-
innerFocused=`colort -l $sign$focusedTint "$outerFocused"`
echo $innerFocused
echo $outerFocused

outerNormal=`bspc config normal_border_color | tr -d \#`
colort -c "$outerNormal" && sign=+ || sign=-
innerNormal=`colort -l $sign$normalTint "$outerNormal"`

# initial draw
_chwb2 Normal  `bspc query -N -n .!descendant_of.window.!fullscreen`
_chwb2 Focused `bspc query -N -n .descendant_of.window.!fullscreen`

# reactive draw
bspc subscribe node_state node_geometry node_focus | while read msg; do
    _chwb2 Normal  `bspc query -N -n .!descendant_of.window.!fullscreen`
    _chwb2 Focused `bspc query -N -n .descendant_of.window.!fullscreen`
done
